使用 python 語法安裝
python -m pip install -U 'channels[daphne]'
在 django_channels/settings.py 中
INSTALLED_APPS = (
"daphne", // 加入此行
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.sites",
...
)
ASGI_APPLICATION = "django_channels.asgi.application" // 加入此行
以上更動主要為要註冊 daphne 至 django 設定裡面
dahpne 是蝦密咚咚? 會在下兩天教到~
建立新腳本 django_channels/asgi.py
加入以下程式碼至 asgi.py 中
import os
from channels.routing import ProtocolTypeRouter
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_channels.settings')
# Initialize Django ASGI application early to ensure the AppRegistry
# is populated before importing code that may import ORM models.
django_asgi_app = get_asgi_application()
application = ProtocolTypeRouter({
"http": django_asgi_app,
# Just HTTP for now. (We can add other protocols later.)
})
加入上述程式碼為使用此 channels 必須條件
接下來三天會在教所需元件的基本介紹, 若想直接看範例: 請直接跳至 [Day 08] Django channels 範例(上)